<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="用户" prop="user">
            <el-input v-model="queryParams.user" placeholder="请输入用户登录名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="账号ID" prop="account">
            <el-input v-model="queryParams.account" placeholder="请输入账号ID" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="变动金额" prop="amount">
            <el-input v-model="queryParams.amount" placeholder="请输入变动金额" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="订单号" prop="orderNo">
            <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="业务类型" prop="bizType">
            <el-select v-model="queryParams.bizType" placeholder="请选择业务类型" clearable style="width: 240px">
              <el-option v-for="dict in biz_account_change_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="资金动向" prop="fundDirection">
            <el-select v-model="queryParams.fundDirection" placeholder="请选择资金动向" clearable style="width: 240px">
              <el-option :key="1" label="增" :value="1" />
              <el-option :key="2" label="减" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['retail:accountHistory:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="accountHistoryList" class="tableEmpty">
        <el-table-column label="用户" align="center" prop="user" />
        <el-table-column label="账号ID" align="center" prop="account" width="150px"/>
        <el-table-column label="变更前余额" align="center" prop="balance">
          <template #default="scope">
            <span>{{ scope.row.balance/100 }} </span>
          </template>
        </el-table-column>
        <el-table-column label="变更前不可用" align="center" prop="unbalance">
          <template #default="scope">
            <span>{{ scope.row.unbalance/100 }} </span>
          </template>
        </el-table-column>
        <el-table-column label="变动金额" align="center" prop="amount">
          <template #default="scope">
            <span>{{ scope.row.fundDirection==1?"+":"-" }}{{ scope.row.amount/100 }} </span>
          </template>
        </el-table-column>
        <el-table-column label="变更后余额" align="center" prop="afterBalance">
          <template #default="scope">
            <span>{{ scope.row.afterBalance/100 }} </span>
          </template>
        </el-table-column>
        <el-table-column label="变更后不可用" align="center" prop="afterUnbalance">
          <template #default="scope">
            <span>{{ scope.row.afterUnbalance/100 }} </span>
          </template>
        </el-table-column>
        <el-table-column label="订单号" align="center" prop="orderNo" />
        <el-table-column label="业务类型" align="center" prop="bizType">
          <template #default="scope">
            <span>
              <dict-tag :options="biz_account_change_type" :value="scope.row.bizType" />
            </span>
          </template>
        </el-table-column>
        <el-table-column label="资金动向" align="center" prop="fundDirection">
          <template #default="scope">
            <span v-if="scope.row.fundDirection==1">增</span>
            <span v-if="scope.row.fundDirection==2">減</span>
            <span v-if="scope.row.fundDirection>2">{{scope.row.fundDirection}}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
          <template #default="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script setup name="AccountHistory" lang="ts">
import { listAccountHistory } from '@/api/retail/accountHistory';
import { AccountHistoryVO, AccountHistoryQuery, AccountHistoryForm } from '@/api/retail/accountHistory/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { biz_account_change_type } = toRefs<any>(proxy?.useDict("biz_account_change_type"));
const accountHistoryList = ref<AccountHistoryVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const accountHistoryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: AccountHistoryForm = {
  id: undefined,
  user: undefined,
  account: undefined,
  amount: undefined,
  balance: undefined,
  afterBalance: undefined,
  orderNo: undefined,
  bizType: undefined,
  fundDirection: undefined,
}
const data = reactive<PageData<AccountHistoryForm, AccountHistoryQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    user: undefined,
    account: undefined,
    amount: undefined,
    balance: undefined,
    afterBalance: undefined,
    orderNo: undefined,
    bizType: undefined,
    fundDirection: undefined,
    params: {
    }
  },
  rules: {
  }
});

const { queryParams, form } = toRefs(data);

/** 查询资金账号历史列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAccountHistory(queryParams.value);
  accountHistoryList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  accountHistoryFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('retail/accountHistory/export', {
    ...queryParams.value
  }, `accountHistory_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
